﻿using System;
using System.Linq;
using System.Xml.Linq;

namespace WolfGame {

  /// <summary>
  /// This class loads the configuration file and sets program parameters.
  /// If no file is present, an exception is thrown and the application closes.
  /// </summary>
  class m_Config {

    #region Constants

    private const string CONFIG_PATH = "{0}\\Resources\\config.xml";

    #endregion

    #region Member Variables

    private Common common;

    #endregion

    #region Constructor

    /// <summary>
    /// Instantiate the class
    /// </summary>
    /// <param name="common"></param>
    /// <param name="args"></param>
    public m_Config(Common common, String[] args) {
      Console.WriteLine("Loading configuration...");
      this.common = common;

      Load();

      // If there are arguments passed in, parse them
      if(args.Length > 0)
        LoadCommandLineArguments(args);
    }

    #endregion

    #region Load Configuration

    /// <summary>
    /// Load all of the paramaters from the configuration file
    /// </summary>
    private void Load() {
      try {
        // Load the file
        string configPath = System.IO.File.ReadAllText(String.Format(CONFIG_PATH,
          System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)));

        XDocument config = XDocument .Parse(configPath);

        Fullscreen = Boolean.Parse((from ss in config.Descendants("screen")
                                    select ss.Attribute("fullscreen").Value).First());
        ScreenWidth = Int32.Parse((from ss in config.Descendants("screen")
                                   select ss.Attribute("width").Value).First());
        ScreenHeight = Int32.Parse((from ss in config.Descendants("screen")
                                    select ss.Attribute("height").Value).First());
        StartFloorName = (from ss in config.Descendants ("floor1")
                          select ss.Attribute ("name").Value).First();
      }
      catch (Exception ex) {
        Console.WriteLine("Error loading configuration file.  " + ex.ToString());
      }
    }

    /// <summary>
    /// Load command line arguments.  These override default values
    /// </summary>
    /// <param name="args"></param>
    private void LoadCommandLineArguments(string[] args) {
      // Loop through each argument
      foreach (string arg in args) {
        // TODO: Implement this
      }
    }

    #endregion

    #region Properties

    public bool Fullscreen { get; set; }
    public int ScreenWidth { get; set; }
    public int ScreenHeight { get; set; }
    public string StartFloorName { get; set; }

    #endregion
  }
}
